/*
 * Copyright 2009 Eat Local Food, LLC
 * Copyright (c) 2007 osCommerce (this file was written after
 * code review of osCommerce).
 *
 * This file is part of gwtCommerce.
 *
 * gwtCommerce is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * gwtCommerce is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with gwtCommerce.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.elf.gwt.store.client.components;

import com.elf.gwt.common.client.model.ProductModel;
import com.elf.gwt.store.client.StoreComponentController;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTMLTable;
import com.google.gwt.user.client.ui.InlineLabel;
import com.google.gwt.user.client.ui.HTMLTable.CellFormatter;

public class BestSellersComponent extends ProductsComponent
{
	protected int prevCategoryId = -1;
	protected int prevManufacturingId = -1;
	protected boolean refresh;

	public BestSellersComponent(StoreComponentController controller, String stylePrefix)
	{
		super(controller, stylePrefix, null, false);
		
		
		setMaxNumCols(1);
		setClearManufacturerId(true);
	}

	/**
	 * BuildData from JSONArray
	 * @param jsonArray
	 */
	protected void buildModel(JSONArray jsonArray) throws Exception
	{
		refresh = false;
		int id = StoreComponentController.categoriesComponent.getCategoryModel().getId();
		int mid = StoreComponentController.manufacturersComponent.getManufacturerModel().getId();
		if (id != prevCategoryId || mid != prevManufacturingId)
		{
			refresh = true;
			prevCategoryId = id;
			prevManufacturingId = mid;
			super.buildModel(jsonArray);
		}
	}

	public void buildFromModel()  throws Exception
	{
		if (refresh)
		{
			int sz = productModels.size();
			bodyPanel = new Grid(sz, 2);
			bodyPanel.setStyleName("ComponentBodyPanel");
			bodyPanel.addStyleName(getHandle() + "BodyPanel");
			bodyPanel.addClickHandler(tableHandler);
			CellFormatter cf = bodyPanel.getCellFormatter();
			for (int i = 0; i < sz; i++)
			{
				ProductModel model = productModels.get(i);
				String row = String.valueOf(i);
				if (i < 10) row = "0" + row;
				InlineLabel inline = new InlineLabel(model.getName());
				inline.addStyleName("index:" + row);
				inline.addMouseOverHandler(mouseOverHandler);
				inline.addMouseOutHandler(mouseOutHandler);
				bodyPanel.setText(i, 0, row + ".");
				bodyPanel.setWidget(i, 1, inline);
				cf.setStyleName(i, 0, getHandle() + "Number");
				cf.setStyleName(i, 1, "ComponentLink");
				cf.addStyleName(i, 1, getHandle() + "Name");
			}
			panel.setWidget(1, 0, bodyPanel);
		}
		onBuilt();
	}

	protected ClickHandler tableHandler= new ClickHandler() {
		public void onClick(ClickEvent event)
		{
			if (dialogBox != null  && dialogBox.isShowing()) dialogBox.hide();
			HTMLTable.Cell cell = bodyPanel.getCellForEvent(event);
			int rowIndex = cell.getRowIndex();
			int index = rowIndex;
			ProductModel productModel = getProductModel(index);

			onSelection("ProductComponent--" + productModel.getId()
					+ "--" + productModel.getCategoryId()
					+ "--" + StoreComponentController.manufacturersComponent.getManufacturerModel().getId());
		}
	};
}
